package cn.iluwen.airline.Services;

import cn.iluwen.airline.Entities.Airport;
import cn.iluwen.airline.Mapper.AirportMapper;
import cn.iluwen.airline.utils.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 机场服务类
 * 处理机场相关的业务逻辑
 */
@Service
public class AirportService {
    
    @Autowired
    private AirportMapper airportMapper;
    
    /**
     * 获取所有机场
     * @return 机场列表
     */
    public List<Airport> getAllAirports() {
        return airportMapper.findAllAirports();
    }
    
    /**
     * 分页获取所有机场
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的机场列表
     */
    public PageInfo<Airport> getAirportsWithPaging(int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Airport> airports = airportMapper.findAirportsWithPaging();
        return PageUtils.getPageInfo(airports);
    }
    
    /**
     * 根据ID获取机场
     * @param airportId 机场ID
     * @return 机场对象
     */
    public Airport getAirportById(String airportId) {
        return airportMapper.findById(airportId);
    }
    
    /**
     * 根据机场三字码获取机场
     * @param airportCode 机场三字码
     * @return 机场对象
     */
    public Airport getAirportByCode(String airportCode) {
        return airportMapper.findByAirportCode(airportCode);
    }
    
    /**
     * 根据机场名称关键词分页查询机场
     * @param airportName 机场名称关键词
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的机场列表
     */
    public PageInfo<Airport> getAirportsByNameLike(String airportName, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Airport> airports = airportMapper.findByAirportNameLike(airportName);
        return PageUtils.getPageInfo(airports);
    }
    
    /**
     * 根据城市名称关键词分页查询机场
     * @param city 城市名称关键词
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页后的机场列表
     */
    public PageInfo<Airport> getAirportsByCityLike(String city, int pageNum, int pageSize) {
        PageUtils.startPage(pageNum, pageSize);
        List<Airport> airports = airportMapper.findByCityLike(city);
        return PageUtils.getPageInfo(airports);
    }
    
    /**
     * 添加机场
     * @param airport 机场对象
     * @return 添加后的机场对象
     */
    public Airport addAirport(Airport airport) {
        // 生成UUID作为主键
        airport.setAirportId(UUID.randomUUID().toString());
        
        // 检查机场代码是否已存在
        Airport existingAirport = airportMapper.findByAirportCode(airport.getAirportCode());
        if (existingAirport != null) {
            throw new RuntimeException("机场代码已存在: " + airport.getAirportCode());
        }
        
        airportMapper.insert(airport);
        return airport;
    }
    
    /**
     * 更新机场信息
     * @param airport 机场对象
     * @return 更新后的机场对象
     */
    public Airport updateAirport(Airport airport) {
        // 检查是否修改了机场代码，如果修改了，需要检查新代码是否已存在
        Airport existingAirport = airportMapper.findById(airport.getAirportId());
        if (!existingAirport.getAirportCode().equals(airport.getAirportCode())) {
            Airport airportWithSameCode = airportMapper.findByAirportCode(airport.getAirportCode());
            if (airportWithSameCode != null) {
                throw new RuntimeException("机场代码已存在: " + airport.getAirportCode());
            }
        }
        
        airportMapper.update(airport);
        return airport;
    }
    
    /**
     * 删除机场
     * @param airportId 机场ID
     * @return 是否删除成功
     */
    public boolean deleteAirport(String airportId) {
        int rows = airportMapper.delete(airportId);
        return rows > 0;
    }
} 